package com.studentrecipe.controller;

import com.studentrecipe.dto.CompleteRecipe;
import com.studentrecipe.mapper.MapperSearch;
import com.studentrecipe.pojo.Recipe;
import com.studentrecipe.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class ControllerSearch {

    @Autowired
    private MapperSearch mapperSearch;

    @RequestMapping("/searchDefault")
    public Result search() {
        List<Recipe> recipes = mapperSearch.searchDefault();
        
//        List<CompleteRecipes> completeRecipes = new ArrayList<>();
//
//        for(Recipes recipe : recipes){
//            CompleteRecipes completeRecipe = new CompleteRecipes();
//            completeRecipe.setRecipes(recipe);
//            completeRecipe.setIngredients(mapperSearch.searchIngredients(recipe.getId()));
//            completeRecipe.setFlavors(mapperSearch.searchFlavors(recipe.getId()));
//            completeRecipes.add(completeRecipe);
//        }
        List<CompleteRecipe> completeRecipes = recipes.stream().map((recipe) -> {
            return new CompleteRecipe(
                    recipe,
                    mapperSearch.searchIngredients(recipe.getId()),
                    mapperSearch.searchFlavors(recipe.getId()),
                    mapperSearch.searchFiles(recipe.getId()));
        }).collect(Collectors.toList());

        return Result.success(completeRecipes);
    }


    @RequestMapping("/searchCondition")
    public Result searchCondition(
            String title, String[] difficulty, String[] cuisine, String[] calorieLevel, String ingredient,
            String[] flavors, LocalDate startTime, LocalDate endTime, Integer smallTime, Integer bigTime)
    {
        List<Integer> recipeIds = mapperSearch.searchCondition(title, difficulty, cuisine, calorieLevel, ingredient, flavors, startTime, endTime, smallTime, bigTime);

        List<CompleteRecipe> completeRecipes = recipeIds.stream().map((recipeId) -> {
            return new CompleteRecipe(
                    mapperSearch.searchRecipe(recipeId),
                    mapperSearch.searchIngredients(recipeId),
                    mapperSearch.searchFlavors(recipeId),
                    mapperSearch.searchFiles(recipeId));
        }).collect(Collectors.toList());

        return Result.success(completeRecipes);
    }

    @RequestMapping("/searchRecipeId")
    public Result searchRecipeId(Integer id){
        CompleteRecipe completeRecipe = new CompleteRecipe(
                mapperSearch.searchRecipe(id),
                mapperSearch.searchIngredients(id),
                mapperSearch.searchFlavors(id),
                mapperSearch.searchFiles(id));
        return Result.success(completeRecipe);
    }

    @RequestMapping("/searchId")
    public Result searchId(Integer id){
        List<Integer> recipeIds = mapperSearch.searchId(id);

        List<CompleteRecipe> completeRecipes = recipeIds.stream().map((recipeId) -> {
            return new CompleteRecipe(
                    mapperSearch.searchRecipe(recipeId),
                    mapperSearch.searchIngredients(recipeId),
                    mapperSearch.searchFlavors(recipeId),
                    mapperSearch.searchFiles(recipeId));
        }).collect(Collectors.toList());

        return Result.success(completeRecipes);
    }

    @RequestMapping("/getRandom")
    public Result getRandom(){
        List<Integer> recipeIds = mapperSearch.searchRandom();

        List<CompleteRecipe> completeRecipes = recipeIds.stream().map((recipeId) -> {
            return new CompleteRecipe(
                    mapperSearch.searchRecipe(recipeId),
                    mapperSearch.searchIngredients(recipeId),
                    mapperSearch.searchFlavors(recipeId),
                    mapperSearch.searchFiles(recipeId));
        }).collect(Collectors.toList());

        return Result.success(completeRecipes);
    }


}
